package GDoc::Job::Fetch::US::Legi::CRS::TMLL;

use Moose;
use WWW::Mechanize;
use Text::Capitalize;
use Data::Dumper;
use Encode 'decode_utf8';

use GDoc::Job;

extends 'GDoc::Job::Fetch::US::Legi::CRS';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://www.law.umaryland.edu/Marshall/crsreports/index.html?AlphaStart=ALL' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{pdf_link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->_fetch_base;
    }
}


sub _fetch_base { 
    my $self = shift;

    $self->logger->info( "Fetching base " . $self->_baseurl );
    my $mech = $self->_mech;
    $mech->get( $self->_baseurl );

    my @links = $mech->find_all_links( url_regex => qr{/crsdocuments/\w+\.pdf$} );

    foreach my $link( @links ) { 
        my $url = $link->url;
        $self->logger->info( "Found PDF link $url" );

        $url =~ m{/(?<num>(RS|RL|IB)\d{5})[-\w]*?\.pdf};
        next unless $+{num};
        my $num  = $+{num};
        my $desc = $link->text;

        # check if we already have it
        my $key = sprintf( "us/legi/crs/CRS-%s.pdf", $num );
        my $s3 = $self->s3;
        my $bkt = $s3->bucket( 'docs.govdoc.org' );
        if ( $bkt->head_key( $key ) ) { 
            $self->logger->info( "Already have $key, skipping" );
            next;
        }

        $desc =~ s/\x{2018}|\x{2019}|\x{201B}/\'/g;
        $desc =~ s/\x{201C}|\x{201D}|\x{201E}|\x{201F}/\"/g;
        $desc =~ s/\x{2013}|\x{2014}/-/g;

        my $job = GDoc::Job->new( class => 'Fetch::US::Legi::CRS::TMLL',
                                  queue => $self->fetch_queue,
                                  data  => { pdf_link     => $url,
                                             num          => $num,
                                             desc         => $desc
                                           }
                                );
        eval { 
            $job->submit;
        };

        if ( my $err = $@ ) { 
            $self->logger->error( "Unable to submit fetch-index job: $err" );
        };
    }
}


1;
